package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Indent;
import com.neusoft.ecommerce.entity.LogisticsStatus;
import com.neusoft.ecommerce.entity.PayWay;
import com.neusoft.ecommerce.entity.UserMessage;
import com.neusoft.ecommerce.service.IndentService;
import com.neusoft.ecommerce.service.LogisticsStatusService;
import com.neusoft.ecommerce.service.PayWayService;
import com.neusoft.ecommerce.service.UserMessageService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class IndentController {
	private final IndentService indentService;
	private final UserMessageService userMessageService;
	private final PayWayService payWayService;
	private final LogisticsStatusService logisticsStatusService;
	
	public IndentController(IndentService indentService,UserMessageService userMessageService,PayWayService payWayService,LogisticsStatusService logisticsStatusService) {
		this.indentService = indentService;
		this.logisticsStatusService = logisticsStatusService;
		this.payWayService = payWayService;
		this.userMessageService = userMessageService;
	}
	@GetMapping("/indent_page")
    public ModelAndView afterSaleServicePage() {
        
         return new ModelAndView("indent/indent-list");
    }
	
    @RequestMapping("/indent/list")
    @ResponseBody
    public LayuiPager<Indent> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Indent> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = indentService.findTotal(pager);
        List<Indent> list = indentService.findList(pager);
        for (Indent indent : list) {
            System.out.println(indent);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addIndent")
    public ModelAndView addIndent() {
        return new ModelAndView("indent/indent-add");
    }
	
	@RequestMapping("/indent/userMessageList")
    public List<UserMessage> findUserMessageList() {
	    return userMessageService.findAll();
    }
	@RequestMapping("/indent/paywayList")
    public List<PayWay> findPaywayList() {
	    return payWayService.findAll();
    }
	@RequestMapping("/indent/logisticsStatusList")
    public List<LogisticsStatus> findLogisticsStatusList() {
	    return logisticsStatusService.findAll();
    }
	
	
	@PostMapping("/indent/add")
    public Map<String, Object> add(Indent indent) {
        System.out.println(indent);
        Map<String, Object> map = new HashMap<>(16);
        if (indentService.add(indent)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/indent/edit_page")
    public ModelAndView editPage(Indent indent, Model model) {
        System.out.println(indent);
        LayuiPager<Indent> pager = new LayuiPager<>(1,1);
        pager.setCondition(indent);
        Indent message = indentService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("indent/indent-edit");
        model.addAttribute("indent", message);
        return view;
    }


	@PostMapping("/indent/edit")
    public Map<String, Object> edit(Indent indent) {
        System.out.println(indent);
        Map<String, Object> map = new HashMap<>(16);
        if (indentService.edit(indent)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/indent/delete")
    public Map<String, Object> delete(Indent indent) {
        System.out.println(indent);
        Map<String, Object> map = new HashMap<>(16);
        if (indentService.delete(indent)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
